﻿using AdminSystem.Application.Admins;
using AdminSystem.Application.Admins.Dto;
using Lazy.Captcha.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace AdminSystem.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "v1")]
    public class AdminController : ControllerBase
    {
        private readonly IAdminService adminService;
        private readonly ICaptcha captcha;

        public AdminController(IAdminService adminService, ICaptcha captcha)
        {
            this.adminService = adminService;
            this.captcha = captcha;
        }


        [HttpPost]
        public async Task<IActionResult> LoginAsync(LoginDto loginDto)
        {
            return Ok(await adminService.LoginAsync(loginDto));
        }

        [HttpGet]
        public IActionResult Captcha()
        {
            var info = captcha.Generate(Guid.NewGuid().ToString("n"));
            // 有多处验证码且过期时间不一样，可传第二个参数覆盖默认配置。
            //var info = _captcha.Generate(id,120);
            var stream = new MemoryStream(info.Bytes);
            return File(stream, "image/gif");
        }

        /// <summary>
        /// 添加方法
        /// </summary>
        /// <param name="createUpdateDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> CreateAsync(CreateUpdateAdminDto createUpdateDto)
        {
            return Ok(await adminService.CreateAsync(createUpdateDto));
        }
    }
}
